package card_discount

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	sharedConv "bp/internal/rpcserver/shared/conv"
	sharedVld "bp/internal/rpcserver/shared/vld"
	cardDiscount "bp/internal/service/entity/card_discount"
	"bp/internal/service/param"
	"bp/internal/util"
	cardDiscountGrpc "bp/rpc/gen/webapi/card_discount"
	"context"
)

// VldCreateCardDiscountReq 验证新建卡折扣请求
func (s *CardDiscountWebApiImp) VldCreateCardDiscountReq(ctx context.Context, q *sqlc.Queries, req *cardDiscountGrpc.CreateCardDiscountReq) (
	ptr *cardDiscount.CreateCardDiscountParam, 
	itemCategoryConfPtrs param.ItemCategoryConfigurationPtrArray,
	itemConfPtrs param.ItemConfigurationPtrArray,
	err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	token := util.GetBilinToken(ctx)

	if !sharedVld.IsOwnerType(req.OwnerType) {
		err = render.InvalidArgumentValue("owner_type")
		return
	}
	if req.OwnerType == string(sqlc.EmOwnerTypeStore) {
		err = render.InvalidArgumentValue("owner_type")
		return
	}
	if req.OwnerId < 0 {
		err = render.InvalidArgumentRange("owner_id")
		return
	}
	if req.OutCardId < 0 {
		err = render.InvalidArgumentRange("out_card_id")
		return
	}
	if util.IsEmpty(req.Name) {
		err = render.ArgumentEmpty("name")
		return
	}
	if !sharedVld.IsOfferCategory(req.OfferCategory) {
		err = render.InvalidArgumentValue("offer_category")
		return
	}
	if req.Discount < 0 || req.Discount > 100 {
		err = render.InvalidArgumentRange("discount")
		return
	}
	if req.NeedVerifyQualification {
		if !sharedVld.IsQualifyTypeArray(req.QualifyTypes, false) {
			err = render.InvalidArgumentValue("qualify_types")
			return
		}
	}
	if !util.IsEmpty(req.DateChooseType) && !sharedVld.IsChooseType(req.DateChooseType) {
		err = render.InvalidArgumentValue("date_choose_type")
		return
	}
	if !sharedVld.IsOfferUseSceneArray(req.UseScenes, false) {
		err = render.InvalidArgumentValue("use_scenes")
		return
	}
	if !sharedVld.IsScope(req.Scope) {
		err = render.InvalidArgumentValue("scope")
		return
	}

	ptr = &cardDiscount.CreateCardDiscountParam{
		UpsertCardDiscountParamsPtr: &sqlc.UpsertCardDiscountParams{
			BrandID:                 token.BrandID,
			OwnerType:               req.OwnerType,
			OwnerID:                 req.OwnerId,
			OutCardID:               req.OutCardId,
			Name:                    req.Name,
			Desc:                    sqlc.NSFromString(req.Desc),
			Tags:                    req.Tags,
			OfferCategory:           req.OfferCategory,
			Discount:                req.Discount,
			NeedVerifyQualification: req.NeedVerifyQualification,
			UseTimeRanges:           sharedConv.ConvTimeRangeArrayPbToEntity(req.UseTimeRanges),
			DateChooseType:          sqlc.NSFromString(req.DateChooseType),
			Weekdays:                req.Weekdays,
			Monthdays:               req.Monthdays,
			UseScenes:               req.UseScenes,
			Scope:                   req.Scope,
		},
	}
	if req.NeedVerifyQualification {
		ptr.UpsertCardDiscountParamsPtr.QualifyTypes = req.QualifyTypes
	}

	itemCategoryConfPtrs = sharedConv.ConvItemCategoryConfigurationPtrArrayPbToEntity(req.ItemCategoryConfs)
	itemConfPtrs = sharedConv.ConvItemConfigurationPtrArrayPbToEntity(req.ItemConfs)

	slog.Infof("card_discount_imp/cmd_vld.VldCreateCardDiscountReq ok")
	return
}

// VldUpdateCardDiscountReq 验证更新卡折扣请求
func (s *CardDiscountWebApiImp) VldUpdateCardDiscountReq(ctx context.Context, q *sqlc.Queries, req *cardDiscountGrpc.UpdateCardDiscountReq) (
	ptr *cardDiscount.UpdateCardDiscountParam, 
	itemCategoryConfPtrs param.ItemCategoryConfigurationPtrArray,
	itemConfPtrs param.ItemConfigurationPtrArray,
	err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	if util.IsEmpty(req.Name) {
		err = render.ArgumentEmpty("name")
		return
	}
	if !sharedVld.IsOfferCategory(req.OfferCategory) {
		err = render.InvalidArgumentValue("offer_category")
		return
	}
	if req.Discount < 0 || req.Discount > 100 {
		err = render.InvalidArgumentRange("discount")
		return
	}
	if req.NeedVerifyQualification {
		if !sharedVld.IsQualifyTypeArray(req.QualifyTypes, false) {
			err = render.InvalidArgumentValue("qualify_types")
			return
		}
	}
	if !util.IsEmpty(req.DateChooseType) && !sharedVld.IsChooseType(req.DateChooseType) {
		err = render.InvalidArgumentValue("date_choose_type")
		return
	}
	if !sharedVld.IsOfferUseSceneArray(req.UseScenes, false) {
		err = render.InvalidArgumentValue("use_scenes")
		return
	}
	if !sharedVld.IsScope(req.Scope) {
		err = render.InvalidArgumentValue("scope")
		return
	}

	ptr = &cardDiscount.UpdateCardDiscountParam{
		CardDiscountID:          req.Id,
		Name:                    req.Name,
		Desc:                    req.Desc,
		Tags:                    req.Tags,
		OfferCategory:           req.OfferCategory,
		Discount:                req.Discount,
		NeedVerifyQualification: req.NeedVerifyQualification,
		UseTimeRanges:           sharedConv.ConvTimeRangeArrayPbToEntity(req.UseTimeRanges),
		DateChooseType:          req.DateChooseType,
		Weekdays:                req.Weekdays,
		Monthdays:               req.Monthdays,
		UseScenes:               req.UseScenes,
		Scope:                   req.Scope,
	}

	if req.NeedVerifyQualification {
		ptr.QualifyTypes = req.QualifyTypes
	}

	itemCategoryConfPtrs = sharedConv.ConvItemCategoryConfigurationPtrArrayPbToEntity(req.ItemCategoryConfs)
	itemConfPtrs = sharedConv.ConvItemConfigurationPtrArrayPbToEntity(req.ItemConfs)

	slog.Infof("card_discount_imp/cmd_vld.VldUpdateCardDiscountReq ok")
	return
}

// VldPauseCardDiscountReq 验证标记卡折扣停用请求
func (s *CardDiscountWebApiImp) VldMarkCardDiscountPausedReq(ctx context.Context, q *sqlc.Queries, req *cardDiscountGrpc.MarkCardDiscountPausedReq) (ptr *cardDiscount.MarkCardDiscountParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	ptr = &cardDiscount.MarkCardDiscountParam{
		CardDiscountID: req.Id,
	}
	slog.Infof("card_discount_imp/cmd_vld.VldMarkCardDiscountPausedReq ok")
	return
}

// VldUnMarkCardDiscountPausedReq 验证取消标记卡折扣停用请求
func (s *CardDiscountWebApiImp) VldUnMarkCardDiscountPausedReq(ctx context.Context, q *sqlc.Queries, req *cardDiscountGrpc.UnMarkCardDiscountPausedReq) (ptr *cardDiscount.MarkCardDiscountParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	ptr = &cardDiscount.MarkCardDiscountParam{
		CardDiscountID: req.Id,
	}
	slog.Infof("card_discount_imp/cmd_vld.VldUnMarkCardDiscountPausedReq ok")
	return
}
